package com.jcl.recruit.api.searchresume.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcl.core.common.page.PageResult;
import com.jcl.core.constants.Constant;
import com.jcl.core.resp.ApiResponse;
import com.jcl.core.web.utils.RequestUtils;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.searchresume.model.SearchResumeRecord;
import com.jcl.recruit.searchresume.service.SearchResumeRecordService;
import com.jcl.recruit.searchresume.vo.SearchResumeRecordVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static org.springframework.beans.support.PagedListHolder.DEFAULT_PAGE_SIZE;

@RestController
@RequestMapping("/api")
public class SearchResumeRecordController {

    @Autowired
    private SearchResumeRecordService searchResumeRecordService;

    @RequestMapping(value = "/searchresume/record/list", method = RequestMethod.GET)
    public ApiResponse list(@RequestParam(value = "page", required = false, defaultValue = "1") String toPage) {

        PageHelper.startPage(Integer.valueOf(toPage), DEFAULT_PAGE_SIZE);
        List<SearchResumeRecord> recordList = searchResumeRecordService.findList(Constant.NUMBER_0);
        PageInfo info = new PageInfo(recordList);
        PageResult result = new PageResult(Integer.valueOf(toPage), null,
                info.getTotal(), info.getList());
        return ApiResponse.success(result);
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping("/searchresume/record/find/{id}")
    public ApiResponse findOne(@PathVariable("id") String id) {
        if (StringUtils.isBlank(id)) {
            return ApiResponse.error("请求路径不合法！", null);
        }
        SearchResumeRecord record = searchResumeRecordService.findOne(Integer.parseInt(id));
        return ApiResponse.success(record);
    }

    /**
     * 用户推荐记录新增
     *
     * @param recordVo
     * @return
     */
    @RequestMapping(value = "/searchresume/record/add", method = RequestMethod.POST)
    public ApiResponse save(@RequestBody SearchResumeRecordVo recordVo) {

        int number = searchResumeRecordService.updateSearchRecord(recordVo);
        if (number == 1) {
            return ApiResponse.success("操作成功");
        } else {
            return ApiResponse.warning("系统已存在相同数据！");
        }
    }

    /**
     * 用户推荐记录修改
     *
     * @param recordVo
     * @return
     */
    @RequestMapping(value = "/searchresume/record/update", method = RequestMethod.POST)
    public ApiResponse update(@RequestBody SearchResumeRecordVo recordVo) {

        if (null == recordVo) {
            return ApiResponse.error("请求参数丢失，找不到修改对象唯一标识");
        }

        int number = searchResumeRecordService.updateSearchRecord(recordVo);
        if (number == 1) {
            return ApiResponse.success("操作成功");
        } else if (number == 2) {
            return ApiResponse.error("修改数据不存在");
        } else {
            return ApiResponse.warning("系统已存在相同数据！");
        }
    }

    /**
     * 假删除用户记录
     *
     * @param id
     * @return
     */
    @RequestMapping("/searchresume/record/delete/{id}")
    public ApiResponse delete(@PathVariable("id") String id) {
        if (StringUtils.isBlank(id)) {
            return ApiResponse.error("请求路径不合法！");
        }
        int number = searchResumeRecordService.deleteById(Integer.parseInt(id));
        return ApiResponse.success(number);
    }

    /**
     * 爬虫爬取的关键词接口
     *
     * @param from
     * @return
     */
    @RequestMapping("/searchresume/keywords")
    public Map<String, Object> keywords(String from) {
        Map<String, Object> map = new HashMap<>();
        String keys = "\"python\", \"爬虫\", \"运营\", \"客服\", \"市场\", \"销售\", \"行政\", \"HR\", \"go\", \"spring\", \"vue\", \"react\", \"前端\", \"android\", \"ios\"";
        String[] kyeslist = keys.replaceAll("\"", "").split(",");
        Random random = new Random();
        String keyword = kyeslist[random.nextInt(14)].trim();
        map.put("type", "success");
        map.put("message", "ok");
        map.put("keywords", keyword);
        return map;
    }
}
